/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vn.com.sml.SEFM.DAO;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.persistence.RollbackException;
import javax.transaction.HeuristicMixedException;
import javax.transaction.HeuristicRollbackException;
import javax.transaction.SystemException;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import vn.com.sml.SEFM.domain.Fraudstandard;
import vn.com.sml.SEFM.util.HibernateUtil;
/**
 *
 * @author Tunp
 */
public class fraudStandardDAO {
    
    public static Boolean update(Fraudstandard fraudStandard) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = null;
        Boolean flag = false;
        try {
            transaction = (Transaction) session.beginTransaction();
            
            session.update(fraudStandard);
            flag = true;
            transaction.commit();
        }
        catch (HibernateException e){
            transaction.rollback();
            e.printStackTrace();
        }
        finally {
            session.close();
        }
        return flag;
    }

    public static List<Fraudstandard> listfraudStandards(String search_col, String search_text) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = null;
        List<Fraudstandard> lst = new ArrayList<Fraudstandard>();
        try {
            transaction = (Transaction) session.beginTransaction();
            
            List lstfraudStandard = session.createQuery("from Fraudstandard where " + search_col + " like '%" + search_text + "%'").list();
            for (Iterator iterator = (Iterator) lstfraudStandard.iterator();iterator.hasNext();)
            {
                Fraudstandard fraudStandard = (Fraudstandard) iterator.next();
               
                lst.add(fraudStandard);
            }
            transaction.commit();
        }
        catch (HibernateException e){
            transaction.rollback();
            e.printStackTrace();
        }
        finally {
            session.close();
        }
        return lst;
    }
    
    public static List<Fraudstandard> listfraudStandards() {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = null;
        List<Fraudstandard> lst = new ArrayList<Fraudstandard>();
        try {
            transaction = (Transaction) session.beginTransaction();
            
            List lstfraudStandard = session.createQuery("from Fraudstandard").list();
            for (Iterator iterator = (Iterator) lstfraudStandard.iterator();iterator.hasNext();)
            {
                Fraudstandard fraudStandard = (Fraudstandard) iterator.next();
               
                lst.add(fraudStandard);
            }
            transaction.commit();
        }
        catch (HibernateException e){
            transaction.rollback();
            e.printStackTrace();
        }
        finally {
            session.close();
        }
        return lst;
    }
    
    public static Fraudstandard getFraudStandardById(int id)
    {
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = null;
        Fraudstandard fraudStandard = null;
        try {
            transaction = (Transaction) session.beginTransaction();
            
            fraudStandard = (Fraudstandard) session.createQuery("from Fraudstandard where fraudstandardId="+id).uniqueResult();
            
            transaction.commit();
        }
        catch (HibernateException e){
            transaction.rollback();
            e.printStackTrace();
        }
        finally {
            session.close();
        }
        return fraudStandard;
    }
}
